package ufr_stgi.assistance_conduite.synchronisation;

import java.util.ArrayList;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import ufr_stgi.assistance_conduite.modele.Panneau;

/**
 * Classe permettant de parser un fichier GPX en utilisant la bibliothèque SAX 
 * afin de l'importer dans la BdD du Smartphone
 * @author Guillaume MONTAVON & Renaud DAVID (Master 2 RIM)
 */
public class PanneauHandler extends DefaultHandler {
	
	private StringBuffer buffer = new StringBuffer();
	
	private ArrayList<Panneau> listePanneau = null;
	private Panneau panneauTemp;
	
	@Override
	public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException {
		
		buffer.setLength(0);
		
		if (localName.equals("gpx") || localName.equals("GPX"))
			listePanneau = new ArrayList<Panneau>();
		else if (localName.equals("wpt") || localName.equals("WPT"))
			panneauTemp = new Panneau(Double.valueOf(atts.getValue("lat")), Double.valueOf(atts.getValue("lon")));
		
	}
	
	@Override
	public void endElement(String uri, String localName, String qName) throws SAXException {
		
		if(buffer.toString().equals(""));
		else if (localName.equals("wpt") || localName.equals("WPT"))
			listePanneau.add(panneauTemp);
		else if (localName.equals("idpan") || localName.equals("IDPAN"))
			panneauTemp.setIdentifiant(Long.valueOf(buffer.toString()));
		else if (localName.equals("idtype") || localName.equals("IDTYPE"))
			panneauTemp.setIdType(Long.valueOf(buffer.toString()));
		else if (localName.equals("fiab") || localName.equals("FIAB"))
			panneauTemp.setFiabilite(Float.valueOf(buffer.toString()));
		else if (localName.equals("nb") || localName.equals("NB"))
			panneauTemp.setNbMarquage(Integer.valueOf(buffer.toString()));
		else if (localName.equals("dir") || localName.equals("DIR"))
			panneauTemp.setDirection(Integer.valueOf(buffer.toString()));
		else if (localName.equals("photo") || localName.equals("PHOTO"))
			panneauTemp.setChemin(buffer.toString());
		
	}
	
	@Override
	public void characters(char[] ch, int start, int length) {
		buffer.append(ch, start, length);
	}
	
	/**
	 * Donne la liste des panneaux créés
	 * @return liste des panneaux créés
	 */
	public ArrayList<Panneau> getListePanneau() {
		return listePanneau;
	}
	
}
